/*******************************************************************************
 * Copyright (c) 2011-2012 Olexandr V. Shaposhnikov.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Olexandr V. Shaposhnikov - initial API and implementation and/or initial documentation
 *******************************************************************************/
package com.funkyroach.use.core.ast;

import java.util.List;

import org.eclipse.dltk.ast.ASTVisitor;
import org.eclipse.dltk.ast.expressions.Expression;

import com.funkyroach.use.core.ast.expr.USMethodCallExpression;

public class USPointedExpression extends Expression {

    private Expression primaryExpression;
    private Expression subExpression;

    public USPointedExpression(Expression astPrimExpr, Expression subExpression) {
        this.primaryExpression = astPrimExpr;
        this.subExpression = subExpression;
    }

    public USPointedExpression(Expression astPrimExpr, @SuppressWarnings("rawtypes") List astNodeList) {
        this.primaryExpression = astPrimExpr;
        if (astNodeList.size() > 1) {
            Expression subExpr = (Expression) astNodeList.get(0);
            astNodeList.remove(0);
            this.subExpression = new USPointedExpression(subExpr, astNodeList);
        } else {
            this.subExpression = (Expression) astNodeList.get(0);
            if (subExpression instanceof USMethodCallExpression) {
                ((USMethodCallExpression) subExpression).setPointedExpression(this);
            }
        }
    }

    @Override
    public int getKind() {
        return 0;
    }

    public void traverse(ASTVisitor visitor) throws Exception {
        if (primaryExpression != null)
            primaryExpression.traverse(visitor);
        if (subExpression != null)
            subExpression.traverse(visitor);
    }

    public Expression getPrimaryExpression() {
        return primaryExpression;
    }

    public void setPrimaryExpression(Expression primaryExpression) {
        this.primaryExpression = primaryExpression;
    }

}
